package com.qy.qDemo.myTest.model

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

/**
 * 类名：qDemo
 * 作者：zcq
 * 时间：2022/2/10 20:47
 * 描述：
 */
class TestModel(countReserved: Int) : ViewModel() {
    val counter: LiveData<Int> get() = _counter
    private val _counter = MutableLiveData<Int>()

    init {
        _counter.value = countReserved
    }

    //主线程
    fun plusOne() {
        val count = _counter.value ?: 0
        _counter.value = count + 1
    }

    //子线程
    fun plusOne2() {
        val count = _counter.value ?: 0
        _counter.postValue(count + 1)
    }

    fun clear() {
        _counter.value = 0
    }
}